﻿// See https://aka.ms/new-console-template for more information
using Quartz;
using Quartz.Impl;
using Quartz.Impl.Matchers;
using Share;

// Grab the Scheduler instance from the Factory

StdSchedulerFactory factory = new StdSchedulerFactory();
IScheduler scheduler = await factory.GetScheduler();

var jobListener = new SampleJobListener();
var triggerListener=new SampleTriggerListener();
scheduler.ListenerManager.AddJobListener(jobListener, KeyMatcher<JobKey>.KeyEquals(SampleJob.Key));
scheduler.ListenerManager.AddTriggerListener(triggerListener, GroupMatcher<TriggerKey>.GroupEquals("group1"));
// and start it off
await scheduler.Start();


// define the job and tie it to our HelloJob class
IJobDetail job = JobBuilder.Create<SampleJob>()
    .WithIdentity(SampleJob.Key)
    .UsingJobData("greeting", "Hello")
    .UsingJobData("user", "Fan")
        .Build();

// Trigger the job to run now, and then repeat every 10 seconds
ITrigger trigger = TriggerBuilder.Create()
    .WithIdentity("trigger1", "group1")
    .StartNow()
    .WithSimpleSchedule(x => x
        .WithIntervalInSeconds(5)
        .RepeatForever())
    .Build();

// Tell Quartz to schedule the job using our trigger
await scheduler.ScheduleJob(job, trigger);


// some sleep to show what's happening
await Task.Delay(TimeSpan.FromSeconds(20));

// and last shut down the scheduler when you are ready to close your program
await scheduler.Shutdown();

Console.WriteLine("Press any key to close the application");
Console.ReadKey();